package com.situation.service.impl;

import com.situation.converter.MenuConverter;
import com.situation.mapper.MenuMapper;
import com.situation.pojo.DO.TbMenu;
import com.situation.pojo.VO.MenuNodeVO;
import com.situation.pojo.VO.MenuVO;
import com.situation.util.MenuTreeBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: 羽程
 * @create: 2021-12-08 15:44
 **/
@Service
public class MenuServiceImpl {
    @Autowired
    private MenuMapper menuMapper;
    /**
     * 添加菜单
     */
    public int addMenu(MenuVO menuVO){
        menuVO.setCreateTime(new Date());
        menuVO.setModifiedTime(new Date());
        return menuMapper.addMenu(menuVO);
    }

    /**
     * 删除菜单
     * @param tbMenu
     * @return
     */
    public int deleteMenu(TbMenu tbMenu){
        return menuMapper.deleteMenu(tbMenu);
    }

    /**
     * 通过ID查询菜单信息
     * @param tbMenu
     * @return
     */
    public TbMenu findMenuById(TbMenu tbMenu){
        return menuMapper.findMenuById(tbMenu);
    }

    /**
     * 修改
     * @param menuVO
     * @return
     */
    public int update(MenuVO menuVO){
        return menuMapper.update(menuVO);
    }

    /**
     * 查询所有
     * @return
     */
    public List<TbMenu> findAllMenu(){
        return menuMapper.findAllMenu();
    }

    /**
     * 加载菜单树（按钮和菜单）
     *
     * @return
     */
    public List<MenuNodeVO> findMenuTree() {
        List<TbMenu> menus = menuMapper.findAllMenu();
        List<MenuNodeVO> menuNodeVOS = MenuConverter.converterToALLMenuNodeVO(menus);
        return MenuTreeBuilder.build(menuNodeVOS);
    }

    /**
     * 获取展开项
     *
     * @return
     */
    public List<Long> findOpenIds() {
        List<Long> ids=new ArrayList<>();
        List<TbMenu> menus = menuMapper.findAllMenu();
        if(!CollectionUtils.isEmpty(menus)){
            for (TbMenu menu : menus) {
                if(menu.getOpen()==1){
                    ids.add(menu.getId());
                }
            }
        }
        return ids;
    }
}
